<div class="rightcontent rc-bg-grey">
    <div class="rc-pagepadding">


        <div class=" rc-pannel ">
            <div class=" rc-pannel-header">
                <div class="rc-flex layui-form toolbar rc-flex-justify-between">
                    <div class=" rc-flex rc-flex-vertical-center ">

                    </div>
                    <div>
                        <a href="javascript:;" id="computeContentCount_btn" class="layui-btn  layui-btn-sm"><i class="iconfont icon-zhishu "></i> 统计栏目文章数</a>
                        <a href="/app/cms/admin/Cate/add" class="layui-btn layui-btn-primary layui-btn-sm"><i class="iconfont">&#xe60e;</i> 添加栏目</a>

                    </div>
                </div>


            </div>


            <div class="rc-pannel-body layui-form rc-pagepadding">

                <table class="layui-hide" id="cates"></table>

            </div>
        </div>
    </div>
</div>


<script type="text/html" id="op-tools">
    <div class="rc-opbar">

        <a class="info" lay-event="addmenu"><i class="iconfont">&#xe785;</i> 加入到导航</a>
        <a class="info" lay-event="edit"><i class="iconfont">&#xe606;</i> 编辑</a>
        <a class="error delete" lay-event="delete"><i class="iconfont">&#xe607;</i> 删除 </a>
    </div>
</script>

<script type="text/html" id="sorttpl">
    <input type="text" class="layui-input catesort" cateid="{{=d.id}}" style="width: 100px;" value="{{= d.sort }}" />
</script>

<script type="text/html" id="comment">
    <input type="checkbox" name="comment" value="{{= d.id }}" title="是|否" lay-skin="switch" lay-filter="comment" {{= d.comment ==1 ? "checked" : "" }}>
</script>

<script type="text/html" id="praise">
    <input type="checkbox" name="praise" value="{{= d.id }}" title="是|否" lay-skin="switch" lay-filter="praise" {{= d.praise ==1 ? "checked" : "" }}>
</script>

<script type="text/html" id="collect">
    <input type="checkbox" name="collect" value="{{= d.id }}" title="是|否" lay-skin="switch" lay-filter="collect" {{= d.collect ==1 ? "checked" : "" }}>
</script>

<script type="text/html" id="contribute">
    <input type="checkbox" name="contribute" value="{{= d.id }}" title="是|否" lay-skin="switch" lay-filter="contribute" {{= d.contribute ==1 ? "checked" : "" }}>
</script>


<script>
    layui.use(function() {

        var layer = layui.layer;
        var form = layui.form;
        var treeTable = layui.treeTable;

        var inst = treeTable.render({
            elem: '#cates',
            treeColIndex: 1,
            url: '/app/cms/admin/Cate/index?type=selcates',
            tree: {
                view: {
                    expandAllDefault: true,
                    iconLeaf: '<i class="layui-icon layui-icon-file"></i>'
                },
                customName: {
                    name: 'title'
                }
            },
            cols: [
                [{
                        field: 'id',
                        title: '编号',
                        width: 20,
                        fixed: 'left'
                    }, {
                        field: 'title',
                        title: '分类名',
                        width: 200,
                        fixed: 'left'
                    },

                    {
                        field: 'modeltitle',
                        title: '所属模型',

                    },
                    {
                        field: 'comment',
                        title: '评论',
                        templet: '#comment'

                    },
                    {
                        field: 'praise',
                        title: '点赞',
                        templet: '#praise'

                    },
                    {
                        field: 'collect',
                        title: '收藏',
                        templet: '#collect'

                    },
                    {
                        field: 'contribute',
                        title: '投稿',
                        templet: '#contribute'

                    },
                    {
                        field: 'sort',
                        title: '排序',
                        templet: '#sorttpl'
                    },


                    {
                        fixed: "right",
                        title: "操作",
                        width: 280,
                        align: "center",
                        toolbar: "#op-tools"
                    }

                ]
            ],

            page: false,


        });


        treeTable.on('tool(' + inst.config.id + ')', function(obj) {

            var layEvent = obj.event; // 获得 lay-event 对应的值
            if (layEvent == 'addmenu') {
                window.open('/admin/HomeMenu/addmenu?title=' + obj.data.title + '&url=/app/cms/home/Index/category?cid=' + obj.data.id);
            }
            if (layEvent === "edit") {
                window.location.href = '/app/cms/admin/Cate/edit?id=' + obj.data.id;

            }
            if (layEvent === "delete") {
                layer.confirm('删除会连同文章一起删除，确定要删除么？', {
                    icon: 3
                }, function() {
                    $.ajax({
                        url: '/app/cms/admin/Cate/delete?id=' + obj.data.id, //请求路径
                        type: "POST", //请求方式
                        success: function(data) {
                            if (data.code == 0) {
                                window.location.reload();
                            } else {
                                toastr_error(data.message);
                            }
                            layer.closeAll();
                        },
                        error: function() {
                            layer.closeAll();
                        },

                    });


                }, function() {



                });

            }
        })

        $('body').on('blur', '.catesort', function() {

            var id = $(this).attr('cateid');
            var sort = $(this).val();

            $.ajax({
                url: '/app/cms/admin/Cate/sort', //请求路径
                type: "POST", //请求方式
                data: {
                    id: id,
                    sort: sort
                }, // 默认写法
                success: function(data) {
                    if (data.code == 0) {
                        toastr_success("排序成功");
                    } else {
                        toastr_error(data.message);
                    }
                },
                error: function() {

                },

            });

        });


        form.on('switch(comment)', function(obj) {
            var id = this.value;
            setInteract('setComment', id, obj.elem.checked);

        });

        form.on('switch(praise)', function(obj) {
            var id = this.value;
            setInteract('setPraise', id, obj.elem.checked);

        });

        form.on('switch(collect)', function(obj) {
            var id = this.value;
            setInteract('setCollect', id, obj.elem.checked);

        });

        form.on('switch(contribute)', function(obj) {
            var id = this.value;
            setInteract('setContribute', id, obj.elem.checked);

        });


        function setInteract(type, id, val) {
            $.ajax({
                url: '/app/cms/admin/Cate/setInteract', //请求路径
                type: "POST", //请求方式
                data: {
                    id: id,
                    type: type,
                    value: val == true ? 1 : 0
                }, // 默认写法
                success: function(data) {
                    if (data.code == 0) {
                        toastr_success("设置成功");
                    } else {
                        toastr_error(data.message);
                    }
                },
                error: function() {

                },

            });

        }



        //计算文章数
        $('#computeContentCount_btn').click(function() {

            layer.load(1);
            $.ajax({
                url: '/app/cms/admin/Cate/computeContentCount', //请求路径
                type: "POST", //请求方式
                data: {

                }, // 默认写法
                success: function(data) {
                    if (data.code == 0) {
                        toastr_success("统计成功");
                    } else {
                        toastr_error(data.message);
                    }
                    layer.closeAll();
                },
                error: function() {
                    layer.closeAll();
                },

            });

        })
    });
</script>